package com.vis.restfs;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest;
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest;

import java.net.URI;
import java.time.Duration;

public class PreUpload {

    public static void main(String[] args) {
        S3Presigner presigner = S3Presigner.builder()
                .endpointOverride(URI.create("http://39.103.63.7:9000"))
                .region(Region.US_EAST_1)
                .credentialsProvider(
                        StaticCredentialsProvider.create(
                                AwsBasicCredentials.create("rustfsadmin", "rustfsadmin")
                        )
                )
                .build();

        PutObjectRequest putRequest = PutObjectRequest.builder()
                .bucket("my-bucket")
                .key("upload.txt")
                .build();

        PresignedPutObjectRequest presignedPut = presigner.presignPutObject(
                PutObjectPresignRequest.builder()
                        .putObjectRequest(putRequest)
                        .signatureDuration(Duration.ofMinutes(10))
                        .build()
        );

        System.out.println("Upload URL: " + presignedPut.url());

    }
}